<?php
namespace app\business\controller;
use app\business\model\BusinessLogin;
use app\business\model\BusinessTable;
use app\business\model\BusinessTableClassify;
use think\Controller;
use app\business\controller\Base;
use think\Session;

class Table extends Base
{
    public function index()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id'];

        $classify = BusinessTableClassify::where('business_id',$num)->select();

        $this->assign('classify',$classify);

        $data = BusinessTable::where('business_id',$num)->select();

        foreach ($data as $k=>$v){
            $data[$k]['table'] = md5($v['table_name']);
        }

        $this->assign('data',$data);

        return $this->fetch();
    }



    public function create()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id'];

        $data = BusinessTableClassify::where('business_id',$num)->select();

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function store()
    {


        $_POST['business_id'] = $_SESSION['think']['business_id'];
        if (!isset($_POST['table_classify'])) {
            return $this->error('请选择分类！');
        }
        //var_dump($_POST);die;
        BusinessTable::create($_POST);

        return redirect('index');
    }

    public function delete($id)
    {
        BusinessTable::where('id',$id)->delete();

        return redirect('index');
    }

    public function changeStatus()
    {
        BusinessTable::update($_POST);

        return redirect('index');
    }

    public function changeAllStatus()
    {



        $business_id = $_SESSION['think']['business_id'];
        //var_dump($business_id);die;
        BusinessTable::where('business_id',$business_id)->update(['table_status'=>1]);

        return redirect('index');
    }

    public function search()
    {
        $search = $_POST['table_name'];

        $type= $_POST['table_classify'];

        $con['table_name']= array('like',"%$search%");

        $con['table_classify']= array('like',"$type");

        $data=BusinessTable::where($con)->select();

        $classify = BusinessTableClassify::all();

        $this->assign('classify',$classify);

        $this->assign('data',$data);

        return $this->fetch('index');
    }

    public function edit($id)
    {
        $info = BusinessTable::where('id',$id)->find();

        $this->assign('info',$info);

        $data = BusinessTableClassify::all();

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function update()
    {
        BusinessTable::update($_POST);

        return redirect('index');
    }
}